<template>
  <div class="login-container">
    <div class="login-bg"></div>
    <el-card class="login-card" :class="{ 'card-animate': animate }">
      <div class="title">
        <i class="el-icon-s-cooperation"></i>
        <span>洗衣服务系统</span>
      </div>
      <el-form :model="loginForm" :rules="loginRules" ref="loginForm">
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username" 
            placeholder="用户名" 
            prefix-icon="el-icon-user"
            @focus="animate = true"
            @blur="animate = false">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input 
            v-model="loginForm.password" 
            placeholder="密码" 
            prefix-icon="el-icon-lock" 
            show-password
            @focus="animate = true"
            @blur="animate = false">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button 
            type="primary" 
            @click="handleLogin" 
            :loading="loading"
            class="login-btn">
            登录
          </el-button>
        </el-form-item>
        <div class="footer">
          <span @click="goToRegister" class="register-link">没有账号？立即注册</span>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { login } from '@/api/user'
import axios from 'axios'

export default {
  name: 'Login',
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      loginRules: {
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      },
      animate: false,
      loading: false
    }
  },
  methods: {
    async handleLogin() {
      try {
        const res = await axios.post('/api/user/login', this.loginForm).then(res => res.data)
        if (res.code === 200) {
          // 存储完整用户对象
          localStorage.setItem('user', JSON.stringify(res.data))
          // 同时单独存储userId，供消息通知组件使用
          localStorage.setItem('userId', res.data.id)
          localStorage.setItem('username', res.data.username)
          
          this.$message.success('登录成功')
          if (res.data.isAdmin) {
            this.$router.push('/admin/home')
          } else {
            this.$router.push('/home')
          }
        } else {
          this.$message.error(res.message || '登录失败')
        }
      } catch (error) {
        this.$message.error('登录请求失败')
      }
    },
    goToRegister() {
      this.$router.push('/register')
    }
  }
}
</script>

<style scoped>
.login-container {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  overflow: hidden;
}

.login-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: linear-gradient(120deg, #a1c4fd 0%, #c2e9fb 100%);
  z-index: -1;
}

.login-card {
  width: 380px;
  padding: 30px 20px;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
}

.card-animate {
  transform: translateY(-5px);
  box-shadow: 0 15px 35px rgba(0, 0, 0, 0.15);
}

.title {
  text-align: center;
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 30px;
  color: #409EFF;
}

.title i {
  margin-right: 10px;
  font-size: 28px;
}

.login-btn {
  width: 100%;
  height: 45px;
  font-size: 16px;
  letter-spacing: 2px;
  border-radius: 25px;
}

.footer {
  text-align: center;
  margin-top: 20px;
}

.register-link {
  color: #909399;
  cursor: pointer;
  font-size: 14px;
  transition: color 0.3s;
}

.register-link:hover {
  color: #409EFF;
}
</style>